<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\RoleService;
use App\Services\PermissionService;

class RoleController extends Controller
{
    private $roleService;
    private $permissionService;

    public function __construct(RoleService $roleService, PermissionService $permissionService)
    {

        $this->roleService = $roleService;
        $this->permissionService = $permissionService;

    }


    public function index()
    {
        $roles = $this->roleService->getRoles();
        return view('backend.role.index', compact('roles'));
    }

    public function create()
    {
        return view('backend.role.create');
    }

    public function store(Request $request)
    {

        if($this->roleService->storeRoles($request->except('_token'))){
            flash('添加成功!', 'success');
            return redirect()->route('admin.role.index');
        }

    }

    public function edit($id)
    {
        $permissions = $this->permissionService->allPermissions();
        return view('backend.role.edit', compact('permissions'))->with('role', $this->roleService->showRole($id));
    }

    public function update($id, Request $request)
    {
        if($this->roleService->updateRoles($id, $request->except(['_method', '_token']))){
            flash('修改成功!', 'success');
            return redirect()->route('admin.role.index');
        }
    }
}
